package com.gx.recruitment.controller;

import com.gx.recruitment.entity.*;
import com.gx.recruitment.service.CompJobService;
import com.gx.recruitment.service.User_favoritesService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class User_favoritesController {
    @Resource
    private User_favoritesService user_favoritesService;
    @Resource
    private CompJobService compJobService;

    @PostMapping("/userfavorites/add")
    public Map<String, Object> insertFavorites(Integer userid, Integer jobid) {
        Map<String, Object> map = new HashMap<>();

        Comp_jobExample example = new Comp_jobExample();
        Comp_jobExample.Criteria cri = example.createCriteria();
        cri.andJobIdEqualTo(jobid);
        List<Comp_job> jobs = compJobService.selectJobByJobId(example);

        User_favorites user_favorites = new User_favorites();
        user_favorites.setJobName(jobs.get(0).getJobName());
        user_favorites.setJobCompany(jobs.get(0).getJobCompany());
        user_favorites.setJobAddress(jobs.get(0).getJobAddress());
        user_favorites.setJobPay(jobs.get(0).getJobPay());
        Date jobdate = new java.sql.Date(new java.util.Date().getTime());
        user_favorites.setJobDate(jobdate);
        user_favorites.setJobId(jobid);
        user_favorites.setUserId(userid);

        User_favoritesExample example1 = new User_favoritesExample();
        User_favoritesExample.Criteria cri1 = example1.createCriteria();
        cri1.andJobIdEqualTo(jobid);
        cri1.andUserIdEqualTo(userid);
        List<User_favorites> listfavos = user_favoritesService.selectByJobIdandUserId(example1);
        if (listfavos.size() > 0) {
            map.put("msg", "已经收藏过了");
        } else {
            int res = user_favoritesService.insertFavorites(user_favorites);
            if (res == 0) {
                map.put("status", 200);
                map.put("msg", "收藏成功");
            } else {
                map.put("msg", "收藏失败");
            }
        }
        return map;
    }

    @PostMapping("/userfavorites/selectFavorites")
    public List<User_favorites> selectFavorites(Integer userid) {
        User_favoritesExample example = new User_favoritesExample();
        User_favoritesExample.Criteria cri = example.createCriteria();
        cri.andUserIdEqualTo(userid);
        List<User_favorites> favorites = user_favoritesService.selectFavorites(example);
        return favorites;
    }

    @PostMapping("/userfavorites/deleteone")
    public Map<String, Object> deleteFavorites(Integer favoritesid) {
        Map<String, Object> map = new HashMap<>();
        int res = user_favoritesService.deleteByfavoritesId(favoritesid);
        if (res == 0) {
            map.put("status", 200);
            map.put("msg", "删除成功");
        } else {
            map.put("msg", "删除失败");
        }
        return map;
    }

    @PostMapping("/userfavorites/deleteByJobId")
    public Map<String, Object> deleteByJobId(Integer jobid) {
        Map<String, Object> map = new HashMap<>();
        User_favoritesExample example = new User_favoritesExample();
        User_favoritesExample.Criteria cri = example.createCriteria();
        cri.andJobIdEqualTo(jobid);
        int res = user_favoritesService.deleteByJobId(example);
        if (res == 0) {
            map.put("status", 200);
            map.put("msg", "收藏删除成功");
        } else {
            map.put("msg", "收藏删除失败");
        }
        return map;
    }
}
